package quickConnect;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JOptionPane;

import org.quickconnect.ControlObject;

import Client.ClientController;

import userInterface.MainFrame;

public class BCOConnectToServer implements ControlObject{
	ObjectOutputStream outputStream;
	@Override
	public Object handleIt(ArrayList<Object> arg0) {
		HashMap params = (HashMap) arg0.get(0);
		
		//Get the controller, IP and Port to connect to
		ClientController controller = MainFrame.mainFrame.getController();
		Socket sock = null;
		String ip = (String) params.get("ip");
		int port = (Integer) params.get("port");
 
		try {
			//Connect
			sock = new Socket(ip,port);
			controller.setSocket(sock);
			controller.setoStream(new ObjectOutputStream(sock.getOutputStream()));
			controller.setiStream(new ObjectInputStream(sock.getInputStream()));
			//MainFrame.status.setMessage("Client: Connected to Server");
			System.out.println("Client: Connected to Server");
			return true;
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "Failed To Contact Server");
			return null;
		}		
	}

}
